package com.edu.d2_stream;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Stream;

public class StreamEmployee {
    /**
     * Stream流案例总结
     */
    public static double sum=0;
    public static double money=0;//两个部门工资

    public static void main(String[] args) {
        List<Employee> list = new ArrayList<>();
        list.add(new Employee("孙悟空",'男',2000,300,"打架"));
        list.add(new Employee("猪八戒",'男',600,700,"睡觉"));
        list.add(new Employee("唐三藏",'男',2000,900,null));
        list.add(new Employee("沙和尚",'男',2000,1000,null));

        List<Employee> list2 = new ArrayList<>();
        list2.add(new Employee("林黛玉",'女',3000,100,null));
        list2.add(new Employee("贾宝玉",'男',500,600,"娘娘腔"));
        list2.add(new Employee("王熙凤",'女',600,700,null));
        list2.add(new Employee("红龙梦",'女',200,400,null));
        list2.add(new Employee("红梦",'女',500,490,null));

        /**
         * 1、找出每个部门，月薪最高的员工，封装成一个TopEmployee
         */
        Employee top=list.stream().max((employee, t1) -> Double.compare(employee.getSalary()+employee.getBonus(),t1.getSalary()+t1.getBonus()))
                .get();
        System.out.println(top);

        TopEmployee Top=list.stream().max((employee, t1) -> Double.compare(employee.getSalary()+employee.getBonus(),t1.getSalary()+t1.getBonus()))
                .map(e -> new TopEmployee(e.getName(),e.getSalary()+e.getBonus())).get();
        System.out.println(Top);
        /**
         * 2、除去最高员工工资、除去最低工资，计算平均工资
         */
/*
        list.stream().sorted(new Comparator<Employee>() {
            @Override
            public int compare(Employee employee, Employee t1) {
                return Double.compare(employee.getSalary()+employee.getBonus(),t1.getSalary()+t1.getBonus());
            }
        }).skip(1).limit(list.size()-2).forEach(new Consumer<Employee>() {
            @Override
            public void accept(Employee employee) {
                sum+=(employee.getSalary()+employee.getBonus());
            }
        });
        System.out.println(sum/(list.size()-2));
*/
       //简便写法
        list.stream().sorted((employee, t1) -> Double.compare(employee.getSalary()+employee.getBonus(),t1.getSalary()+t1.getBonus()))
                .skip(1).limit(list.size()-2).forEach(e->{
                    sum+=(e.getSalary()+e.getBonus());
        });
        System.out.println("第一个部门的均工资是："+sum/(list.size()-2));

        System.out.println("-----------------------");
        /**
         * 计算两个部门整体的平均工资，去掉最低工资、去掉最高工资
         */
        /**
         * 将两个流合并，然后再进行操作
         */
        Stream<Employee> stream1=list.stream();
        Stream<Employee> stream2=list2.stream();
        Stream<Employee> concat = Stream.concat(stream1, stream2);
        concat.sorted((employee, t1) -> Double.compare(employee.getSalary()+employee.getBonus(),t1.getSalary()+t1.getBonus()))
                .skip(1).limit(list.size()+list2.size()-2).forEach(e->{
            money+=(e.getSalary()+e.getBonus());
        });
        System.out.println("两个部门的均工资是："+money/(list.size()+list2.size()-2));
        /**
         * 为解决精度问题，使用BigDecimal进行封装
         */
        BigDecimal decimal = BigDecimal.valueOf(money);
        BigDecimal decimal1 = BigDecimal.valueOf(list.size() + list2.size() - 2);
        //divide是BigDecimal自带的加减乘除规则中的除法
        System.out.println("两个部门人均工资为："+decimal.divide(decimal1,2, RoundingMode.HALF_UP));

    }
}
